package com.engr.web;

import com.engr.annotation.FreeAccess;
import com.engr.base.BaseController;
import com.engr.consts.ResultCodes;
import com.engr.core.service.member.MemberService;
import com.engr.domain.MemberLogin;
import com.engr.dto.MemberDto;
import com.engr.util.ResponseMessage;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 * @program: train-project
 * @description: 会员应用层
 * @author: lyx
 * @create: 2018-11-03
 **/
@RestController
@RequestMapping(value = "/api/member",name = "会员操作类",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE,MediaType.APPLICATION_JSON_VALUE})
public class MemberRestController extends BaseController {


    @Autowired
    private MemberService memberService;

    @FreeAccess
    @PostMapping("/login")
    @ApiOperation(value = "用户登录",notes = "用户登录,用户名或者手机号或者邮箱必填")
    public ResponseMessage login(@Valid @RequestBody MemberDto memberDto, BindingResult bindingResult){

        if(bindingResult.hasErrors())
            return ResponseMessage.error(ResultCodes.PARAM_ERROR, "参数异常", memberDto);
        MemberLogin memberLogin = new MemberLogin();
        Date now = new Date();
        memberLogin.setLoginTime(now);
        boolean result = memberService.login(memberDto, memberLogin);
        if(!result){
            return ResponseMessage.error("登录失败");
        }
        return ResponseMessage.success("登录成功");

    }

    @FreeAccess
    @GetMapping("/validateCode")
    @ApiOperation(value = "验证码接口",notes = "腾讯人机验证码")
    public ResponseMessage validateCode(@RequestParam String ticket,@RequestParam String randStr){

        boolean result=memberService.getVcode(ticket, randStr, request());
        if(!result){
            ResponseMessage.error("请求失败");
        }
        return ResponseMessage.success("请求成功");
    }

}
